package team.szm.scd.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.szm.scd.database.entity.User;
import team.szm.scd.database.mapper.UserMapper;
import team.szm.scd.service.interfaces.IUserService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User fromId(String id) {
        User user = userMapper.selectById(id);
        if (user != null)
            log.trace("查询到的User: " + user);
        else log.trace("没有查询到User");
        return user;
    }

    @Override
    public User fromId2(String id2) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid2", id2);
        List<User> users = userMapper.selectByMap(map);
        if (users != null && users.size() >= 1) {
            return users.get(0);
        }
        return null;
    }

    /**
     * 向数据加入一条用户信息
     *
     * @param user 要加入的用户对象
     * @return true表示成功
     */
    @Override
    public boolean addUser(User user) {
        return userMapper.insert(user) == 1;
    }
}
